<template>

  <el-dialog :title="id ? '返佣人修改' : '返佣人添加'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="140px" >
      <el-form-item label="返佣人" size="mini" prop="name">
        <el-input placeholder="" v-model="form.name" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <el-form-item label="银行卡号" size="mini" prop="cardNo">
        <el-input placeholder="" v-model="form.cardNo" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <el-form-item label="联系方式" size="mini" prop="phone">
        <el-input placeholder="" v-model="form.phone" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <el-form-item label="remark" size="mini" prop="remark">
        <el-input placeholder="" v-model="form.remark" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">提 交</el-button>
    </div>

  </el-dialog>
</template>

<script>

import ApiMain from '@/api/bank.channelIntroducer';
export default {
  mixins: [
  ],
  props: [
  ],
  data () {
    return {

      dialogVisible: false,
      popDeleteVisible: false,
      loading: false,

      form: {
        name: '',
        cardNo: '',
        phone: '',
        remark: '',
      },

      rules: {
        name: [
          { required: true, message: '请填写返佣人', trigger: 'blur' },
          { min: 2, max: 15, message: '2-15个字符', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请填写手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确' }
        ]
      },

      id  : null,
      data: null,

    }
  },
  mounted () {
  },

  methods: {

    show (id = null) {

      this.id = id;

      if (this.id) {
        this.fetchData();
      }
      else {
        this.reset();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        name: '',
        cardNo: '',
        phone: '',
        remark: '',
      };
    },

    fetchData () {

      ApiMain.info({id: this.id})
      .then(re => {
        this.data = re; 
      })
      .catch(() => {
        this.data = null;
      })
      .finally(() => {
        this.renderData();
      });
    },

    renderData () {
      this.form.name = this.data.name;
      this.form.cardNo = this.data.cardNo;
      this.form.phone = this.data.phone;
      this.form.remark = this.data.remark;
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitDel() {

      this.popDeleteVisible = false;
      this.loading = true;

      await ApiMain.del({id: this.id});

      this.loading = false;
      this.close();
      this.$message({message: '删除成功', type: 'success'});
    },

    async submitSet () {

      this.form.id = this.id;
      let form = Object.copy(this.form)
      await ApiMain.set(form);
      this.$message({message: '修改成功', type: 'success'});
      this.close();

    },

    async submitAdd () {
      let form = Object.copy(this.form)
      await ApiMain.add(form);
      this.$message({message: '添加成功', type: 'success'});
      this.close();
    },
  }
}
</script>

<style>

</style>
